cmake_minimum_required(VERSION 2.8)
project(rtc_xswitch2)

option(LIBEXECINFO_STANDALONE "if libexecinfo is standalone" OFF)
message("Option LIBEXECINFO_STANDALONE=${LIBEXECINFO_STANDALONE}")

include(CPack)


if (CMAKE_VERSION VERSION_LESS "3.1")
    add_definitions(-std=c++11)
    add_definitions(-Wall -g -ggdb -Werror=return-type)

    if (NOT APPLE)
        add_definitions(-static-libgcc -static-libstdc++)
    endif (NOT APPLE)
else ()
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    set(CMAKE_CXX_EXTENSIONS OFF)

    SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -Wall -g -ggdb -Werror=return-type")
    SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall -g -ggdb -DNDEBUG -Werror=return-type")

    if (NOT APPLE)
        set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
    endif (NOT APPLE)
endif ()

set(CMAKE_VERBOSE_MAKEFILE yes)

add_definitions(-DRAPIDJSON_HAS_STDSTRING=1)
add_definitions(-DRAPIDJSON_BUILD_DOC=0)
add_definitions(-DRAPIDJSON_BUILD_EXAMPLES=0)
add_definitions(-DRAPIDJSON_BUILD_TESTS=0)
add_definitions(-DHAS_DTLS_WINDOW_SIZE=1)
add_definitions(-Wunused-function)

include_directories(src)
include_directories(third/webrtc)

find_package(PkgConfig REQUIRED)
#pkg_check_modules(LIBEVENT REQUIRED libevent)
pkg_check_modules(LOG4CPLUS REQUIRED log4cplus)
pkg_check_modules(SRTP REQUIRED libsrtp2)
pkg_check_modules(NICE REQUIRED nice)
pkg_check_modules(GLIB REQUIRED glib-2.0)
pkg_check_modules(GOBJECT REQUIRED gobject-2.0)
pkg_check_modules(GIO REQUIRED gio-2.0)
pkg_check_modules(OPUS REQUIRED opus)
pkg_check_modules(LIBSOUP REQUIRED libsoup-2.4)
pkg_check_modules(OPENSSL REQUIRED openssl)
pkg_check_modules(RAPIDJSON REQUIRED RapidJSON)
pkg_check_modules(X264 REQUIRED x264)
# pkg_check_modules(LIBFRIBIDI REQUIRED fribidi)
pkg_check_modules(LIBFONTCONFIG REQUIRED fontconfig)
pkg_check_modules(VPX REQUIRED vpx)
pkg_check_modules(BRAEKPAD_CLIENT REQUIRED breakpad-client)

#message("LOG4CPLUS_INCLUDE_DIRS=${LOG4CPLUS_INCLUDE_DIRS}")
#message("LOG4CPLUS_LIBRARY_DIRS=${LOG4CPLUS_LIBRARY_DIRS}")
#message("LOG4CPLUS_LDFLAGS=${LOG4CPLUS_LDFLAGS}")
#message("SRTP_INCLUDE_DIRS=${SRTP_INCLUDE_DIRS}")
#message("SRTP_LIBRARY_DIRS=${SRTP_LIBRARY_DIRS}")
#message("SRTP_LDFLAGS=${SRTP_LDFLAGS}")
message("OPENSSL_INCLUDE_DIRS ${OPENSSL_INCLUDE_DIRS}")
message("OPENSSL_LIBRARIES ${OPENSSL_LIBRARIES}")
message("OPENSSL_LIBRARY_DIRS ${OPENSSL_LIBRARY_DIRS}")
message("OPENSSL_LDFLAGS ${OPENSSL_LDFLAGS}")
message("OPENSSL_LDFLAGS_OTHER ${OPENSSL_LDFLAGS_OTHER}")
message("OPENSSL_CFLAGS ${OPENSSL_CFLAGS}")
message("OPENSSL_CFLAGS_OTHER ${OPENSSL_CFLAGS_OTHER}")

include_directories(
        ${LOG4CPLUS_INCLUDE_DIRS}
        ${GLIB_INCLUDE_DIRS}
        ${GOBJECT_INCLUDE_DIRS}
        ${GIO_INCLUDE_DIRS}
        ${OPENSSL_INCLUDE_DIRS}
        ${OPUS_INCLUDE_DIRS}
        ${LIBSOUP_INCLUDE_DIRS}
        ${SRTP_INCLUDE_DIRS}
        ${NICE_INCLUDE_DIRS}
        ${RAPIDJSON_INCLUDE_DIRS}
        ${LIBFRIBIDI_INCLUDE_DIRS}
        ${LIBFONTCONFIG_INCLUDE_DIRS}
        ${VPX_INCLUDE_DIRS}
        ${BRAEKPAD_CLIENT_INCLUDE_DIRS}
)

link_directories(
        ${LOG4CPLUS_LIBRARY_DIRS}
        ${OPENSSL_LIBRARY_DIRS}
        ${OPUS_LIBRARY_DIRS}
        ${LIBSOUP_LIBRARY_DIRS}
        ${SRTP_LIBRARY_DIRS}
        ${NICE_LIBRARY_DIRS}
        ${X264_LIBRARY_DIRS}
        ${LIBFRIBIDI_LIBRARY_DIRS}
        ${LIBFONTCONFIG_LIBRARY_DIRS}
        ${VPX_LIBRARY_DIRS}
        ${BRAEKPAD_CLIENT_LIBRARY_DIRS}
)

add_subdirectory(app/bench)
add_subdirectory(app/merge)
add_subdirectory(app/tlv-analyzer)
add_subdirectory(app/rtc-signal)
add_subdirectory(app/rtc-xswitch)
add_subdirectory(app/transfmt)
add_subdirectory(app/udp-relay)
#add_subdirectory(app/rtc-test)
#add_subdirectory(app/demo)
add_subdirectory(app/srtp-decoder)


# if (APPLE)
# endif (APPLE)


# fails to build in docker
# caused by av_err2str: taking pointer from temporary array
#add_subdirectory(app/tlv)
#add_subdirectory(app/mixer)
#add_subdirectory(app/srtp-decoder)
#add_subdirectory(app/gsocket-echo-server)

#file(GLOB WEBRTC_SRCS third/webrtc *.cpp *.hpp *.cc *.h)
#add_library(webrtc ${WEBRTC_SRCS})

get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
    message(STATUS "dir='${dir}'")
endforeach()

set(CPACK_SET_DESTDIR ON)
set(CPACK_INSTALL_PREFIX "/opt") 
set(CPACK_PACKAGE_VERSION "2.2.x")
set(CPACK_RPM_PACKAGE_GROUP "easemob") 
set(CPACK_PACKAGE_VENDOR "easemob")
set(CPACK_RPM_PACKAGE_LICENSE "")
set(CPACK_GENERATOR "TZ")
# 
# include(GetPrerequisites)  
# 
# get_prerequisites(${BINARY_LOCATION} DEPENDENCIES 0 0 "" "")
# foreach(DEPENDENCY_FILE ${DEPENDENCIES})
#   get_filename_component(DEPENDENCY_NAME "${DEPENDENCY_FILE}" NAME)
#   get_filename_component(DEPENDENCY "${DEPENDENCY_FILE}" REALPATH)
#   get_filename_component(DEPENDENCY_PATH "${DEPENDENCY}" DIRECTORY)
#   install(FILES ${DEPENDENCY} DESTINATION "${DEPENDENCY_PATH}" )
# endforeach()
# 

